<?php

namespace App\Admin\Actions\Keyword;

use App\Miya\Models\Area;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class DerivativeForKeyword extends RowAction
{
    public $name = '生成衍生词';

    public function handle(Model $model)
    {
        $areas = Area::select(['id', 'name'])->get();
        $insertData = app('admin_keyword')->buildDerivative($model, $areas);
        $insertDataArray = array_chunk($insertData, 100);
        foreach ($insertDataArray as $data) {
            DB::table('derivative_keywords')->insertOrIgnore($data);
        }

        return $this->response()->success('衍生词生成完成')->refresh();
    }
}
